package com.hyxt.DTO;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hyxt.api.T809ClientService;
import com.hyxt.api.T809ServerService;
import com.hyxt.dao.RedisServiceDAO;
import com.hyxt.utils.IRedisMessageListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author shenp
 * @version v1.0
 * @Description 监听t809发送消息
 * @Date: Create in 15:47 2017/11/28
 * @Modifide By:
 **/
@Component
public class T809ReMessageListener  extends IRedisMessageListener {
    @Reference(version = "1.0.0")
    T809ClientService t809ClientService;
    @Reference(version = "1.0.0")
    T809ServerService t809ServerService;
    @Reference(version = "1.0.0")
    RedisServiceDAO redisServiceDAO;
    @Override
    public void onMessage(String channel, String message) {
        System.out.println(channel);
       JSONObject jsonObject = JSON.parseObject(message, JSONObject.class);
        //唯一标识
        Integer sign = (Integer) jsonObject.get("sign");
        //主从标识
        Integer mainOrBranch = (Integer)jsonObject.get("mainOrBranch");
        //原始数据
        String datas = (String)jsonObject.get("datas");
        byte[] bytes = datas.getBytes();
        String position =(String) redisServiceDAO.getMap(sign+mainOrBranch+"",0).get("position");
        if ("client".equals(position)){
            t809ClientService.sendMessage(sign,mainOrBranch,bytes);
        }else{
            t809ServerService.sendMessage(sign,mainOrBranch,bytes);
        }
    }
}
